package cn.yh.nft.turbo.order.domain.service;

import cn.yh.nft.turbo.api.order.constant.TradeOrderState;
import cn.yh.nft.turbo.api.order.request.OrderCreateRequest;
import cn.yh.nft.turbo.api.order.response.OrderResponse;
import cn.yh.nft.turbo.order.domain.entity.TradeOrder;
import cn.yh.nft.turbo.order.domain.listener.event.OrderCreateEvent;
import cn.yh.nft.turbo.order.infrastructure.mapper.OrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esotericsoftware.kryo.util.Null;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import java.util.Date;

/**
 * @ClassName: OrderReadService
 * @Author: 草莓爱上啵啵
 */
@Service
public class OrderReadService extends ServiceImpl<OrderMapper, TradeOrder> {

    @Autowired
    private OrderMapper orderMapper;



    /**
     * 按照订单号查询订单信息
     * @param orderId
     * @return
     */
    public TradeOrder getOrder(String orderId){
        return orderMapper.selectByOrderId(orderId);
    }

    public TradeOrder getOrder(String orderId, String buyerId){
        return orderMapper.selectByOrderIdAndBuyer(orderId, buyerId);
    }

    public Page<TradeOrder> pageQueryByState(String buyerId, String state, int currentPage, int pageSize) {
        Page<TradeOrder> page = new Page<>(currentPage, pageSize);
        page = this.page(page, new LambdaQueryWrapper<TradeOrder>()
                .eq(TradeOrder::getBuyerId, buyerId)
                .eq(StringUtils.isNotBlank(state), TradeOrder::getOrderState, state)
                .orderByDesc(TradeOrder::getGmtCreate)
        );

        //根据状态、时间、用户id分页查询
        return page;
    }


    /**
     * 分页查询已过时订单
     * @param currentPage
     * @param pageSize
     * @param buyerIdTailNumber 买家id的尾号
     * @return
     */
    public Page<TradeOrder> pageQueryTimeoutOrders(int currentPage, int pageSize, @Nullable String buyerIdTailNumber){
        Page<TradeOrder> page = new Page<>(currentPage, pageSize);
        page = this.page(page, new LambdaQueryWrapper<TradeOrder>()
                .in(TradeOrder::getOrderState, TradeOrderState.CONFIRM.name(), TradeOrderState.CREATE.name())
                .in(TradeOrder::getGmtCreate, DateUtils.addMinutes(new Date() , -TradeOrder.DEFAULT_TIME_OUT_MINUTES))
                .likeLeft(StringUtils.isNotBlank(buyerIdTailNumber), TradeOrder::getBuyerId, buyerIdTailNumber)
        );

        //根据状态、时间、用户id分页查询
        return page;
    }

    /**
     * 分页查询待确认订单
     * @param currentPage
     * @param pageSize
     * @param buyerIdTailNumber
     * @return
     */
    public Page<TradeOrder> pageQueryNeedConfirmOrders(int currentPage, int pageSize, @Nullable String buyerIdTailNumber){
        Page<TradeOrder> page = new Page<>(currentPage, pageSize);
        page = this.page(page, new LambdaQueryWrapper<TradeOrder>()
                .isNull(TradeOrder::getOrderConfirmedTime)
                .orderByDesc(TradeOrder::getGmtCreate)
                .likeLeft(StringUtils.isNotBlank(buyerIdTailNumber), TradeOrder::getBuyerId, buyerIdTailNumber)
        );

        //根据状态、时间、用户id分页查询
        return page;
    }

}
